<?php
namespace app\model;



class Order {
    static function checkorder($themeid,$config,$system,$uid = ''){
        global $_G;
        if ($uid='') {
            $uid = $_G['member']['uid'];
        }
        $record = db_find ( "SELECT * FROM " . tablename (t_record) . " WHERE themeid = '{$themeid}' and uid = '{$uid}' and status = 0 and fee>0 order by id desc" );

        if(!empty($record)){
            $order = db_find ( "SELECT * FROM " . tablename (t_order) . " WHERE themeid = '{$themeid}' and uid = '{$_G['member']['uid']}' and orderfee>0 order by id desc" );
            $checkresult = checkWechatPayByOrderNo($order['tid'],$system);

            if($checkresult['errcode'] == 0 && $order['orderfee']==$checkresult['fee']){
                $config =   Theme::getConfig($order['themeid']);
                $chajia = sprintf("%.2f",floatval($config['groupprice']-$config['primeprice']));
                if (($order['orderfee']>0 && $checkresult['fee'] == $order['orderfee'] && ($order['orderfee'] == $config['joinfee'] || $order['orderfee'] == $config['primeprice'] ||  $order['orderfee'] == $chajia || $order['orderfee'] == $config['groupprice'] || $order['orderfee'] == $config['groupprice2'] || $order['orderfee'] == $config['joinfee'] || $order['orderfee'] == $config['joinfee2'] || $order['orderfee'] == $config['joinfee3']) ) || ($config['yukuan']>0 && TIMESTAMP>$config['yukuantime'])){ 
                    $record = Record::getById($order['masterid']);
                    $relationtable = Relation::getByThemeId($order['themeid'],$record['openid']);

                    $data = array(
                        'paystate'=>1,
                        'status'  =>1,
                        'workerid'  =>$relationtable['workerid'],
                        'realfee'  =>$checkresult['fee'],
                    );
                    
                    $Recordstate = Record::update($order['masterid'],array('status'=>1));
                    if ($Recordstate) {
                        $Orderstate = Order::update($order['id'],$data);
                        if ($Orderstate) {
                            Pay::commonBiz($system,$order['masterid'],$order['themeid'],$data['realfee']);
                        }
                    }
                }
            }
        }
    }

    static function getByTId($tid) {
        if (empty($tid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where tid = :tid";
        $row = db_find($sql, array(":tid"=>$tid));
        return $row;
    }


    static function save($data){
        global $_G;
        $insertid = db_insert(t_order, $data);
        $id = $insertid;
        return $id;
    }


    static function getById($id) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where id = :id";
        $row = db_find($sql, array(":id"=>$id));
        return $row;
    }

    static function update($id,$data){
        global $_G, $_P;
        if($id <= 0){
            return false;
        }
        return db_update (t_order, $data, array("id"=>$id) );
    }


    static function getPayOrderByRId($recordid) {
        if (empty($recordid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where ordertype = 'theme' and masterid = :masterid order by id limit 1";
        $row = db_find($sql, array(":masterid"=>$recordid));
        return $row;
    }


    static function getPayOrderByRIdZhifude($recordid) {
        if (empty($recordid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where paystate = 1 and realfee>0 and masterid = :masterid order by id limit 1";
        $row = db_find($sql, array(":masterid"=>$recordid));
        return $row;
    }

    static function getRefundOrderByRId($recordid) {
        if (empty($recordid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where ordertype = 'refund' and masterid = :masterid order by id limit 1";
        $row = db_find($sql, array(":masterid"=>$recordid));
        return $row;
    }

    static function setCheckById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_order, array("hascheck"=>1), array ('id' => $id) );
    }

    static function setHasNoticeById($id,$hasnotice = 1) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_order, array("hasnotice"=>$hasnotice), array ('id' => $id) );
    }

    static function setCostAndLeftFeeById($costfee,$leftfee,$sendfee=0,$id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_order, array("costfee"=>$costfee,"leftfee"=>$leftfee,"sendfee"=>$sendfee), array ('id' => $id) );
    }

    static function setIsDistributorById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_order, array("isdistributor"=>1), array ('id' => $id) );
    }

    static function setEndDistributorById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_order, array("isdistributor"=>2), array ('id' => $id) );
    }

    static function deleteById($id){
        if (empty($id)) {
            return false;
        }
        db_delete ( t_order, array ('id' => $id) );
        return true;
    }


    static function getListByCondition($condition,$pageNumber,$pageSize){
        $sql = "SELECT master.* FROM " . tablename ( t_order ) . " as master 
						WHERE   {$condition} ORDER BY master.id desc
									LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
        $list = db_select ( $sql );
        return $list;

    }

    static function getIncomeByThemeId($themeid){
        $income = db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_order ) . "
				WHERE themeid = :themeid  and paystate = 1 and ordertype = 'theme' ", array(":themeid"=>$themeid) );
        return round(floatval($income),2);
    }

    static function getOutcomeByThemeId($themeid){
        $income = db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_order ) . "
				WHERE themeid = :themeid  and paystate = 1 and ordertype in ('comm','refund') ", array(":themeid"=>$themeid) );
        return round(floatval($income),2);
    }

    static function getRealByThemeId($themeid){
        $income = db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_order ) . "
				WHERE themeid = :themeid  and paystate = 1 and ordertype in ('comm','theme','refund') ", array(":themeid"=>$themeid) );
        return round(floatval($income),2);
    }

    static function getRealByThemeIdnew($themeid){
        global $_G;
        $income = floatval(db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_order ) . "
                WHERE themeid = '{$themeid}' and paystate = 1 and realfee > 0 " ));
        $outcome = floatval(db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename ( t_comm )  . "
                WHERE themeid = '{$themeid}'  and status  = 1  " ));
        // $income = db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_order ) . "
        //         WHERE themeid = :themeid  and paystate = 1 ", array(":themeid"=>$themeid) );
        $shouxu = 1 - Order::shouxufei();
        return round(floatval($income*$shouxu-$outcome),2);
    }

    static function shouxufei(){
        global $_G;
        $isold = false;
        $themeid = Theme::getDefaultThemenew();
        if (!empty($themeid)) {
            $theme = Theme::getThemeById($themeid);
            if ($theme['softsn']=='1') {
                $isold = true;
            }
        }
        if ($system['s']['domain1'] == "www.yixuan.net" || DOMAIN0 == "19.yixuan.net") {
            $isold = true;
        }
        return $isold?0.006:0.007;
    }

    static function getRealByThemeIdcomm4($themeid){
        global $_G;
        $theme = Theme::getThemeById($themeid);
        $total =  Order::getRealByThemeIdnew($themeid);
        if ($theme['begintime']>strtotime("-3 day")) {
            $total = $total + 29;
        }
        return $total ;
    }

    static function getRealByThemeIdRefund($themeid){
        global $_G;
        $income = floatval(db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_order ) . "
                WHERE themeid = '{$themeid}' and paystate = -1 and realfee > 0 " ));
        return $income;
    }
    static function getAttendNumByThemeIdAndStatus($themeid,$status = 1){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_order ) . " as master
					WHERE  themeid = :themeid and status = :status ORDER BY master.id", array(":themeid"=>$themeid,":status"=>$status) );
        return intval($num);
    }

    static function getAttendNumByThemeId($themeid){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_order ) . " as master
					WHERE  themeid = :themeid and status > 0 ORDER BY master.id", array(":themeid"=>$themeid) );
        return intval($num);
    }

    static function getAttendListByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_order ) . " as master 
						WHERE   themeid = :themeid and status > 0 ORDER BY master.id desc LIMIT 16";
        $list = db_select ( $sql , array(":themeid"=>$themeid));
        return $list;

    }


    static function getNumByThemeIdAndStatus($themeid,$status=0){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_order ) . " as master
					WHERE  themeid = :themeid and status = :status ORDER BY master.id", array(":themeid"=>$themeid,":status"=>$status) );
        return intval($num);
    }



    static function getNumByOpenid($openid){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_order ) . " as master
					WHERE  openid = :openid ORDER BY master.id", array(":openid"=>$openid) );
        return intval($num);
    }

    static function getNumByCondition($condition){
        return db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_order ) . " as master
					WHERE  {$condition} ORDER BY master.id" );

    }



    static function getByIdAndShopid($id,$shopid) {
        if (empty($id) || empty($shopid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where id = :id and shopid = :shopid";
        $row = db_find($sql, array(":id"=>$id,":shopid"=>$shopid));
        return $row;
    }

    static function getByThemeIdAndOpenid($themeid,$openid) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where themeid = :themeid and openid = :openid";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getByIdAndOpenid($id,$openid) {
        if (empty($id) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " where id = :id and openid = :openid";
        $row = db_find($sql, array(":id"=>$id,":openid"=>$openid));
        return $row;
    }


    static function getFixListByOpenidOrMobile($openid,$mobile,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_order) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.status >= 0 and r.type = 'repair' and r.uniacid = :uniacid and r.openid = :openid";
        $p = array(":uniacid"=>$uniacid,":openid"=>$openid);
        if(!empty($mobile)){
            $sql.=" or r.regmobile = :mobile";
            $p[":mobile"] = $mobile;
        }
        $sql .= " order by r.id desc limit 50";

        $list = db_select($sql,$p );
        return $list;

    }


    static function getUnNoticeList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_order) . " as r
                        where r.uniacid = :uniacid and r.status = 0 and r.hasnotice = 0";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getCheckList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_order) . " as r
                        where r.uniacid = :uniacid and r.status >= 1 and r.fee > 0 and r.hascheck = 0 order by r.id desc";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getDistributorList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_order) . " as r
                        where r.uniacid = :uniacid and r.status >= 1 and r.fee > 0 and r.hascheck = 1 and r.isdistributor = 1 order by r.id desc";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getListByShopid($shopid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_order) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getIsOpenListByShopid($shopid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_order) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.isopen = 1
                                and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getListByShopidAndWorker($shopid,$wopenid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_order) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.wopenid =:wopenid and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid,":wopenid"=>$wopenid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getListByOpenidOrMobile($openid,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img,s.title as shop,s.mobile as kfdh FROM " . tablename(t_order) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        left join " . tablename(t_shop) . " as s on r.shopid = s.id
                        where r.status >= 0 and r.uniacid = :uniacid and r.openid = :openid order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":openid"=>$openid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getListByMobile($mobile,$uniacid){
        if (empty($mobile)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_order) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.status >= 0 and r.uniacid = :uniacid and r.regmobile = :regmobile order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":regmobile"=>$mobile);
        $list = db_select($sql,$p );
        return $list;

    }


    static function getCancelByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $p = array(":openid"=>$openid,":uniacid"=>$uniacid);
        $p[':addtime'] = TIMESTAMP - 7*24*60*60;
        $hascancel = db_fetchcolumn ( "SELECT COUNT(*) FROM " . tablename (t_order) . "
								WHERE uniacid = :uniacid and status = -1 and openid =:openid and addtime > :addtime",$p);
        return $hascancel;
    }

    static function getUndealFixByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " 
                    where uniacid = :uniacid and type = 'repair' and status = 0 and openid =:openid limit 1";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }

    static function getUndealRecycleByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_order) . " 
                    where uniacid = :uniacid and type = 'recycle' and status = 0 and openid =:openid limit 1";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }

    static function getTotalByStatus($status,$uniacid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_order ) . " WHERE uniacid = :uniacid and status = :status";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getTotalByStatusByShopid($status,$uniacid,$shopid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid,":shopid"=>$shopid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_order ) . " WHERE uniacid = :uniacid and status = :status and shopid =:shopid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getTotalByStatusByWorker($status,$uniacid,$shopid,$openid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid,":shopid"=>$shopid,":openid"=>$openid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_order ) . " WHERE uniacid = :uniacid and status = :status and shopid =:shopid and wopenid = :openid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }


    static function getTotalByShopid($shopid){
        if(empty($shopid)){
            return false;
        }
        $p = array(":shopid"=>$shopid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_order ) . " WHERE shopid = :shopid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getByUId($uid) {
        if (empty($uid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename("mc_members") . " where uid = :uid";
        $row = db_find($sql, array(":uid"=>$uid));
        return $row;
    }
    static function getByOpenid($openid){
        global $_G, $_P;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':openid'] = $openid;
        $fans = db_find("SELECT * FROM ".tablename(t_worker)." WHERE openid = :openid",$params);
        return $fans;
    }

    static function getShopWorkerByOpenid($openid,$shopid){
        global $_G, $_P;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':openid'] = $openid;
        $params[':shopid'] = $shopid;
        $row = db_find("SELECT * FROM ".tablename(t_worker)." WHERE shopid = :shopid and openid = :openid",$params);
        return $row;
    }



}
